package com.uwosh.cs342.g6.mapapp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.uwosh.cs342.g6.mapapp.MainActivity.checkFriendRequests;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Favorite extends Activity {
	
	TextView latView; 
	TextView lonView; 
	
	Context c;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favorite);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.favorite, menu);
		
		latView = (TextView) findViewById(R.id.latText);
		lonView = (TextView) findViewById(R.id.lonText);

		Intent intent = getIntent();
		Double lat = Double.parseDouble(intent.getStringExtra("lat"));
		Double lon = Double.parseDouble(intent.getStringExtra("lon"));

		c = this;

		latView.setText(lat + "");

		

		lonView.setText(lon + "");

		return true;
	}

	public void save(View v) 
	{
		TextView user = (TextView)findViewById(R.id.nameText);
		CharSequence x = user.getText();
		String name = x.toString();
		
		x = latView.getText();
		double la = Double.parseDouble(x.toString().trim());
		
		x = lonView.getText();
		double lo = Double.parseDouble(x.toString().trim());
		
		if(name.equals(""))
		{
			Toast.makeText(getApplicationContext(), "Enter a name please", Toast.LENGTH_SHORT).show();
		}
		else
		{
			String[] temp = new String[3];
			temp[0] = name;
			temp[1] = la+"";
			temp[2] = lo+"";
			new saveFavorite().execute(temp);
		}
		
	}
	
	@Override
	public void onBackPressed() {
	   this.finish();
	}

	public class saveFavorite extends AsyncTask<String, Context, String> {

		@Override
		protected String doInBackground(String... params) {

			String name = params[0];
			Double lat = Double.parseDouble(params[1]);
			Double lon = Double.parseDouble(params[2]);
			
			ArrayList<NameValuePair> postParameters;
			
			postParameters = new ArrayList<NameValuePair>();
			

			postParameters.add(new BasicNameValuePair("latitude",lat+""));
			postParameters.add(new BasicNameValuePair("longitude",lon+""));
			postParameters.add(new BasicNameValuePair("id",Login.currentUserId+""));
			postParameters.add(new BasicNameValuePair("name",name));



			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
					"http://softeng.cs.uwosh.edu/students/cs342g6/saveFavorite.php");
			try {
				httppost.setEntity(new UrlEncodedFormEntity(postParameters));
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		    try {

		        // Execute HTTP Post Request

		        ResponseHandler<String> responseHandler = new BasicResponseHandler();
		        String response = httpclient.execute(httppost, responseHandler).trim();
		        
		        if(Integer.parseInt(response) == 1)
		        {
		        	return "true";
		        }
		        else
		        {
		        	return "false";
		        }
		        
		    } catch (ClientProtocolException e) {
		        //Toast.makeText(params[0], "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
		        // TODO Auto-generated catch block
		    } catch (IOException e) {
		        //Toast.makeText(params[0], "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
		        // TODO Auto-generated catch block
		    }
			return "false";
		    

		}

		@Override
		protected void onPostExecute(String values) {

			if(values.equals("true"))
			{
				Toast.makeText(c, "Favorite Saved", Toast.LENGTH_SHORT).show();

			}

		}
	}

}
